<template>
  <div>
    <el-row>
      <el-col>
        <!-- 分界线-->
        <el-divider>
          <span style=" font-size: 25px; font-weight: bold;color: #67C23A;">美丽乡村旅游网修改密码</span>
        </el-divider>

        <el-form :model="ruleForm" status-icon ref="ruleForm" class="login-box" :rules="rules">
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-unlock" type="password" show-password placeholder="请输入旧密码"
              v-model="ruleForm.password" autocomplete="off" clearable></el-input>
          </el-form-item>

          <el-form-item prop="newPassword">
            <el-input type="password" show-password prefix-icon="el-icon-unlock" placeholder="请输入新密码"
              v-model="ruleForm.newPassword" autocomplete="off" clearable></el-input>
          </el-form-item>

          <el-form-item prop="newPassword2">
            <el-input type="password" show-password prefix-icon="el-icon-unlock" placeholder="请再次确认新密码"
              v-model="ruleForm.newPassword2" autocomplete="off" clearable></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="confirm">确定修改</el-button>
          </el-form-item>

          <!-- 底部按键-->
          <el-form-item>
            <el-button type="text" size="small" style="float: left; " @click="bach">返回</el-button>
            <el-button type="text" @click="register" size="small" style="float: right;">立即注册</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import newAxios from "@/utils/axios.js";

export default {
  name: "loginUser",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'));
      } else {
        if (this.ruleForm.newPassword2 !== '') {
          this.$refs.ruleForm.validateField('newPassword2');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'));
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        newPassword: "",
        newPassword2: "",
        password: "",
      },

      rules: {
        password: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        newPassword: [
          { validator: validatePass, trigger: 'blur,change' },
          {
            pattern: /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{8,}$/,
            message: "密码必须包含英文字母和数字，且长度大于或等于8位",
            trigger: "blur",
          },
        ],
        newPassword2: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      },
      myInfo: {},
    };
  },
  created() {
    this.getUser();
  },
  methods: {
    //跳转至注册页面
    register() {
      console.log("用户跳转至注册页面");
      this.$router.push({ path: "/register" });
    },

    //跳转上一界面
    bach() {
      this.$router.back();
    },
    getUser() {
      newAxios
        .get("/admin/findUserById")
        .then((res) => res.data)
        .then((res) => {
          if (res.code == 200) {
            console.log(res.data);
            this.myInfo = res.data;
          }
        })
        .catch((err) => {
          this.$message.error("发送错误");
        });
    },
    confirm() {
      if (Object.keys(this.myInfo).length > 0) {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            newAxios
              .post("/admin/updatePassword", {
                ...this.ruleForm,
              })
              .then((res) => {
                if (res.data.code == 200) {
                  this.$message.success("密码修改成功！请重新登陆")
                  this.$router.push("/loginUser");
                } else {
                  this.$message.error("旧密码输入错误！！");
                  return false;
                }
              });
          } else {
            this.$message({
              message: "请检查数据是否正确",
              type: "warning",
            });
          }
        });
      } else {
        this.$message.error("请先登陆噢亲！")
      }
    },
  },
};
</script>

<style scoped>
.login-box {
  border: 1px solid #dcdfe6;
  width: 350px;
  margin: 0 auto;
  margin-top: 100px;
  padding: 50px 50px 10px 50px;
  border-radius: 5px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  box-shadow: 0 0 25px #909399;
}
</style>
